const express = require('express');
let router = new express.Router();
const organDAO = require('../dao/OrganDAO')
const url = require('url');


router.get('/organ',(req,res)=>{
  let promise=organDAO.select()
  promise.then((data)=>{
    res.json({
      data:data
    });
  },(err)=>{
    console.log("err:"+err);
  })
})

router.get('/select',(req,res)=>{
  let promise=organDAO.query(req.query.dept_name,req.query.dept_address)
  promise.then((data)=>{
    res.json({
      errno:0,
      data:data
    });
  },(err)=>{
    console.log('err:'+err);
  })
});

router.get('/add',(req,res)=>{
  let organ={
    dept_name:req.query.dept_name,
    dept_count:req.query.dept_count,
    superior:req.query.superior,
    isUsed:req.query.isUsed,
    dept_address:req.query.dept_address
  }
  let promise=organDAO.add(organ);
  promise.then((data)=>{
    res.json({
      errno:0,
      data:data
    });
  },(err)=>{
    console.log('err:'+err);
  })
})

router.get('/update',(req,res)=>{
  let organ={
    dept_name:req.query.dept_name,
    dept_count:req.query.dept_count,
    superior:req.query.superior,
    isUsed:req.query.isUsed,
    dept_address:req.query.dept_address,
    dept_id:req.query.dept_id
  }
  let promise=organDAO.update(organ);
  promise.then((data)=>{
    res.json({
      errno:0,
      data:data
    });
  },(err)=>{
    console.log('err:'+err);
  })
})

module.exports=router;
